package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {


    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Override
    public ApartmentItemVo getApartmentItemVoById(Long apartmentId) {
        // 根据公寓ID查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(apartmentId);

        // 根据公寓ID查询标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(apartmentId);

        // 根据公寓ID查询图表信息列表
       List<GraphInfo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, apartmentId);

        // 查询公寓的最低租金
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        // 创建公寓项VO对象
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        // 将ApartmentInfo对象的属性复制到ApartmentItemVo对象
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        // 设置图表信息列表
        apartmentItemVo.setGraphVoList(graphVoList);
        // 设置标签信息列表
        apartmentItemVo.setLabelInfoList(labelInfoList);
        // 设置最低租金
        apartmentItemVo.setMinRent(minRent);

        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(id);
        ObjectUtils.isEmpty(apartmentInfo);
        //查询图表信息
        List<GraphInfo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //查询标签信息
        List<LabelInfo> labelInfoList =labelInfoMapper.selectListByApartmentId(id);
        //查询公寓的配置信息
        List<FacilityInfo> facilityInfoList =facilityInfoMapper.selectListByApartmentId(id);
        //查询公寓的最低房租
        BigDecimal minRent =roomInfoMapper.selectMinRentByApartmentId(id);

        ApartmentDetailVo appapartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo,appapartmentDetailVo);
        appapartmentDetailVo.setIsDelete(apartmentInfo.getIsDeleted()==1);
        appapartmentDetailVo.setLabelInfoList(labelInfoList);
        appapartmentDetailVo.setGraphVoList(graphVoList);
        appapartmentDetailVo.setFacilityInfoList(facilityInfoList);
        appapartmentDetailVo.setMinRent(minRent);


        return appapartmentDetailVo;
    }
}




